<?php

namespace app\forms\user;

use yii\db\Exception;
use app\forms\Model;
use app\models\User;
use app\models\Position;
use yii\web\HttpException;
use app\validators\PositiveInteger;

class PositionItemForm extends Model
{
    public $id;

    public function rules()
    {
        return [
            ['id', PositiveInteger::class],
            ['id', 'required'],
            ['id', 'exist', 'targetClass' => User::class],
        ];
    }

    /**
     * @param $param
     * @return mixed
     * @throws HttpException
     */
    public static function getItem($param)
    {
        $form = new self;
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';

        try {

            $user = User::findOne($form['id']);



            return (new Position)->getItem($user['position_id']);

        } catch (Exception $e) {
            $form->serverError();
        }

    }
}